package cn.liumouren.tool.security.demo.config;

import cn.liumouren.tool.security.autoconfigure.SecurityProperties;
import cn.liumouren.tool.security.autoconfigure.component.filter.CheckTokenFilter;
import cn.liumouren.tool.security.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 配置demo
 *
 * @author Daniel Liu
 * @date 2021/3/5 17:03
 */
@Configuration(proxyBeanMethods = false)
public class SecurityConfig {

    //===============================================================================
    // 必须添加!!    TokenProcessor
    //===============================================================================

    @Bean
    public TokenProcessor tokenProcessor() {
        // 以 , 分隔得到permissions
        return token -> {
            Permission permission = new DefaultPermission();
            permission.add(token.split(","));
            return permission;
        };
    }

//    @Bean
//    public TokenProcessor tokenProcessor() {
//         // 使用jwt得到permissions
//        return token -> {
//            List<String> permissions = JwtUtil.parse(token)
//                    .getClaim(JwtUtil.DEFAULT_PERMISSIONS_KEY)
//                    .asList(String.class);
//            Permission permission = new DefaultPermission();
//            permission.addAll(permissions);
//            return permission;
//        };
//    }


    //===============================================================================
    // 添加Filter  (非必须)
    //===============================================================================

    @Bean
    public DlFilter xxFilter() {
        DlFilter filter = new DlFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                System.out.println("进入xxFilter...");
                filterChain.doFilter(request, response);
            }
        };
        // 作用的路径 /**可以匹配所有路径
        filter.addPatterns("/**");
        // 排除的路径 /* dl-security2里做了处理, 可以匹配 /user, /admin, /admin/
        // 但不能匹配 /admin/login
        filter.excludePatterns("/*");
        // 设置过滤器执行顺序, 默认0, 值越优先级越高
        filter.setOrder(10);

        return filter;
    }

    /**
     * 自定义 CheckTokenFilter
     */
    @Bean
    public CheckTokenFilter ctFilter(SecurityProperties properties, TokenProcessor tokenProcessor) {
        CheckTokenFilter filter = new CheckTokenFilter(properties, tokenProcessor);

        filter.addPatterns("/**");
        filter.excludePatterns("/swagger-ui.html");

        return filter;
    }


    //===============================================================================
    // 添加Interceptor  (非必须)
    //===============================================================================

    @Bean
    public DlInterceptor xxInterceptor() {
        DlInterceptor interceptor = new DlInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                System.out.println("进入xxInterceptor....");
                return true;
            }
        };
        // 与DlFilter同理
        interceptor.addPatterns("/**");
        // 与DlFilter同理
        interceptor.excludePatterns("/*");
        // 设置拦截器执行顺序, 默认0, 值越优先级越高
        interceptor.setOrder(-1);

        return interceptor;
    }

}
